<?php
class ControllerSaleCouponcard extends AdminController {

  	public function index() {  

		M('sale/couponcard');
       $this->data['method']='index';
		$this->getList();
  	}


  	public function insert() {

	  	M('sale/couponcard');
    	if ($this->request->isPost()&&($this->validateInsert())) {
			$this->model_sale_couponcard->addCouponcard($this->request->post);

			$this->session->set_flashdata('success', $this->_['text_success_insert']);
			$this->redirect(UA('sale/couponcard'));
    	}
     
    	$this->getForm();
  	}


  	public function update() {

		 M('sale/couponcard');

    	if ($this->request->isPost()) {
    		$datas=array();
    		for($i=0;$i<count($this->request->post)/3;$i++){
    			$datas[$i]=array(
    				'lowAmount'=>$this->request->post['lowAmount'.$i],
    				'highAmount'=>$this->request->post['highAmount'.$i],
    				'reward'=>$this->request->post['reward'.$i],
    				);
    			$this->data['couponcards'][$i]=$datas[$i];
    		}

    		if($this->validateUpdate($datas)){

               $this->model_sale_couponcard->clearCouponcard();
               $this->model_sale_couponcard->addCouponcards($datas);

               $this->session->set_flashdata('success', $this->_['text_success_update']);
               $this->redirect(UA('sale/couponcard'));
               }

	   	}
      
    	  $this->getForm2();
  	}



    public function delete() {

		M('sale/couponcard');

    	if (isset($this->request->post['selected']) ) {
         $this->model_sale_couponcard->deleteCouponcard($this->request->post['selected']);
         $this->session->set_flashdata('success', $this->_['text_success_delete']);
	     $this->redirect(UA('sale/couponcard'));
    	}
    	
    
    	$this->getList();
  	}

   

  	protected function getList() {
		$this->document->setTitle($this->_['heading_title']);

		$qf = new Query_filter();

		$this->data['couponcards'] = array();

		$couponcard_total = $this->model_sale_couponcard->getTotalCouponcards();//获取总条数

		$results = $this->model_sale_couponcard->getCouponcards($qf);

    	foreach ($results as $result) {
			  $this->data['couponcards'][] = array(
			  	'couponID'  => $result['couponID'],
				'lowAmount' => $result['lowAmount'],
				'highAmount'  => $result['highAmount'],
				'reward'     => $result['reward']
			);
		}

		$this->data['pagination'] = $this->pagination($couponcard_total)->render();
		$this->display('sale/couponcard_list.tpl');
  	}


  	protected function getForm(){
  		$this->document->setTitle($this->_['heading_title']);

			$this->data['action'] = UA('sale/couponcard/insert');
			$this->data['lowAmount'] =P('lowAmount');
			$this->data['highAmount'] = P('highAmount');
			$this->data['reward'] = P('reward');

       
        	$results = $this->model_sale_couponcard->getCouponcards();
    	foreach ($results as $result) {
			  $this->data['couponcards'][] = array(
			  	'couponID'  => $result['couponID'],
				'lowAmount' => $result['lowAmount'],
				'highAmount'  => $result['highAmount'],
				'reward'     => $result['reward']
			);
        }
		
		$this->display('sale/couponcard_form.tpl');

  	}


  	protected function getForm2(){
  		$this->document->setTitle($this->_['heading_title']);

		$this->data['action'] = UA('sale/couponcard/update');

		$results = $this->model_sale_couponcard->getCouponcards();
		if(!$this->request->isPost()){
		$i=0;
    	foreach ($results as $result) {
			  $this->data['couponcards'][$i] = array(
			  	'couponID'  => $result['couponID'],
				'lowAmount' => $result['lowAmount'],
				'highAmount'  => $result['highAmount'],
				'reward'     => $result['reward']
			);
			  $i++;
		}
       }
		$this->display('sale/couponcard_form_update.tpl');
  	}




    protected function validateInsert() {
	    $pass = true;

	    if(!is_numeric($this->request->post['lowAmount'])||!is_numeric($this->request->post['highAmount'])||!is_numeric($this->request->post['reward'])||$this->request->post['lowAmount']>$this->request->post['highAmount']){
	    	$this->setMessage('error_entry', $this->_['error_entry']);
			$pass = false;
	    }

	     M('sale/couponcard');

	     $results = $this->model_sale_couponcard->getCouponcards();

	     foreach ($results as $result) {
	     	if(($result['lowAmount']<=$this->request->post['lowAmount']&&$this->request->post['lowAmount']<=$result['highAmount'])||($result['lowAmount']<=$this->request->post['highAmount']&&$this->request->post['highAmount']<=$result['highAmount'])){
	     		$this->setMessage('error_entry', $this->_['error_entry']);
			    $pass = false;
	     	}
		}

    	return $pass;
  	}


  	 protected function validateUpdate($datas) {

  	 	foreach ($datas as $result) {

  	 		foreach ($datas  as $result2) {
  	 			  if($result2!==$result){
  	 			  	 if(($result['lowAmount']<=$result2['lowAmount']&&$result['lowAmount']<=$result2['highAmount'])||($result['highAmount']<=$result2['highAmount']&&$result['highAmount']<=$result2['highAmount'])){
	     		          $this->setMessage('error_entry', $this->_['error_entry']);
			                return false;
	     	         }
  	 			  }
  	 		}
  	 	}

    	return true;
  	}
 
}
?>